<template>
  <div class="shareImgManagement">
    <h3>分享图片管理</h3>
    <div class="opration">
      <el-button type="success" icon="el-icon-plus" @click="addData" v-if="permissions_obj.id_169||permissions_obj.id_all">添加</el-button>
    </div>
    <div class="list">
      <el-table
        border
        ref="multipleTable"
        :data="tableData"
        tooltip-effect="dark"
        style="width: 100%"
      >
        <el-table-column type="index" width="50" header-align="center" align="center" label="序号"></el-table-column>
        <el-table-column header-align="center" align="center" prop="num" label="图片">
          <template slot-scope="scope">
            <el-image
              style="width: 100px; height: 100px"
              :src="scope.row.cover"
              :preview-src-list="scope.row.srcList"
              fit="“cover”"
            ></el-image>
          </template>
        </el-table-column>
        <el-table-column header-align="center" align="center" prop="content" label="文案"></el-table-column>
        <el-table-column header-align="center" align="center" prop="created_at" label="添加时间"></el-table-column>
        <el-table-column header-align="center" align="center" label="操作" fixed="right">
          <template slot-scope="scope">
            <el-button size="mini" type="warning" @click="handleEdit(scope.$index, scope.row)" v-if="permissions_obj.id_170||permissions_obj.id_all">编辑</el-button>
            <el-button size="mini" type="success" @click="handleDelete(scope.$index, scope.row)" v-if="permissions_obj.id_171||permissions_obj.id_all">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="page">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page.sync="currentPage1"
        :page-sizes="[10,30,50,100]"
        :page-size="pageSize"
        layout="total, prev, pager, next,sizes,jumper"
        :total="totalSize"
      ></el-pagination>
    </div>
    <el-dialog :title="dialogTitle" :visible.sync="addDialogVisible" width="40%" center>
      <div class="row" v-if="isShowId">
        <span>ID：</span>
        <el-input placeholder="请输入用户邀请码" v-model="inputId" clearable disabled></el-input>
      </div>
      <div class="row">
        <span>上传图片：</span>
        <el-upload
          :action="imgUploadParth"
          list-type="picture-card"
          :on-preview="handlePictureCardPreview"
          :limit="1"
          :on-success="imgUploadSuccess"
          :on-error="imgUploadFailure"
          :file-list="fileList"
        >
          <i class="el-icon-plus"></i>
        </el-upload>
        <el-dialog :visible.sync="dialogVisible">
          <img width="100%" :src="dialogImageUrl" alt />
        </el-dialog>
      </div>
      <div class="row">
        <span>添加文案：</span>
        <el-input
          v-model="inputText"
          placeholder="文案长度不超过80个字符"
          clearable
          show-word-limit
          maxlength="80"
          type="textarea"
        ></el-input>
      </div>
      <div class="dialogBtn">
        <el-button type="primary" size="small" @click="submitModifyEditor">提交</el-button>
        <el-button type="info" size="small" @click="cancelModifyEditor">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import md5 from "js-md5";
export default {
  name: "shareImgManagement",
  data() {
    return {
      currentPage1: 1,
      pageSize: 10,
      totalSize: 0,
      tableData: [],
      addDialogVisible: false,
      dialogTitle: "添加分享图片",
      uploadImg: "",
      //图片上传接口地址
      imgUploadParth: "",
      fileList: [],
      dialogImageUrl: "",
      dialogVisible: false,
      inputId: "",
      inputText: "",
      initData: {},
      isShowId: false,
      // 权限管理
      permissions: [],
      permissions_obj: {},
    };
  },
  created() {
    //权限判断
    this.permissions = this.$store.state.permissions;
    let oneOff = false;
    this.permissions.forEach((item, index) => {
      if (item == -1) {
        oneOff = true;
      } else {
        this.permissions_obj["id_" + item] = item;
      }
    });
    if (oneOff) {
      this.permissions_obj.id_all = true;
    } else {
      this.permissions_obj.id_all = false;
    }
  },
  mounted() {
    this.getFileUploadParth();
    this.getDataList();
  },
  methods: {
    // 分页
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.currentPage1 = val;
      this.getDataList();
    },
    handleSizeChange(val){
      this.pageSize=val;
      this.getDataList();
    },
    addData() {
      this.isShowId = false;
      this.inputText = "";
      this.uploadImg="";
      this.fileList=[];
      this.addDialogVisible = true;
      this.dialogTitle = "添加分享图片";
    },
    handleEdit(index, row) {
      this.fileList=[];
      this.isShowId = true;
      this.inputId = row.id;
      this.inputText = row.content;
      this.addDialogVisible = true;
      this.dialogTitle = "修改分享图片";
    },
    //图片预览
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    //图片上传成功
    imgUploadSuccess(response, file, fileList) {
      this.$message({
        message: "上传成功",
        type: "success"
      });
      this.uploadImg = response.result.file_path;
    },
    imgUploadFailure(err, file, fileList) {
      this.$message({
        message: "上传失败",
        type: "warning"
      });
    },
    //获取文件上传地址
    async getFileUploadParth() {
      let parames = {
        handshake: "",
        data: {}
      };
      let res = await this.$axios.post(
        this.$api.uploadGet_domain,
        JSON.stringify(parames)
      );
      if (res.code == 200) {
        let timestamp = parseInt(new Date().getTime() / 1000);
        let platform = "P";
        let secret = "Z6Kbz8hpT9VtGmcu";
        let sign = md5(platform + timestamp + secret).toUpperCase();
        // console.log(sign);
        this.imgUploadParth =
          res.data.url +
          "?dir=ad&platform=P&timestamp=" +
          timestamp +
          "&sign=" +
          sign;
      } else {
        this.$message({
          message: res.message,
          type: "warning"
        });
      }
    },
    //获取初始化列表数据
    async getDataList() {
      let parames = {
        handshake: "",
        data: {
          page: this.currentPage1,
          page_size: this.pageSize
        }
      };
      let res = await this.$axios.post(
        this.$api.shareLists,
        JSON.stringify(parames)
      );
      if (res.code == 200) {
        this.initData = res.data;
        this.initData.data.forEach((item, index) => {
          let arr = [];
          arr.push(item.cover);
          item.srcList = arr;
        });
        this.totalSize = this.initData.total;
        this.tableData = this.initData.data;
      } else {
        this.$message({
          message: res.message,
          type: "warning"
        });
      }
    },
    //提交
    submitModifyEditor() {
      if (this.dialogTitle == "添加分享图片") {
        this.addMethod();
      } else {
        this.modifyMethod();
      }
    },
    cancelModifyEditor() {
      this.addDialogVisible = false;
    },
    //添加接口
    async addMethod() {
      if (!this.inputText) {
        this.$message({
          message: "请填写文案",
          type: "warning"
        });
        return ;
      }
      let parames = {
        handshake: "",
        data: {
          cover: this.uploadImg,
          content: this.inputText
        }
      };
      let res = await this.$axios.post(
        this.$api.shareAdd,
        JSON.stringify(parames)
      );
      if (res.code == 200) {
        this.$message({
          message: res.message,
          type: "success"
        });
        this.addDialogVisible = false;
        this.getDataList();
      } else {
        this.$message({
          message: res.message,
          type: "warning"
        });
      }
    },
    //编辑接口
    async modifyMethod() {
      let parames = {
        handshake: "",
        data: {
          id: this.inputId,
          cover: this.uploadImg,
          content: this.inputText
        }
      };
      let res = await this.$axios.post(
        this.$api.shareUpdate,
        JSON.stringify(parames)
      );
      if (res.code == 200) {
        this.$message({
          message: res.message,
          type: "success"
        });
        this.addDialogVisible = false;
        this.getDataList();
      } else {
        this.$message({
          message: res.message,
          type: "warning"
        });
      }
    },
    //删除
    handleDelete(index, row) {
      this.$confirm("是否继续该操作?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.deleteMethod(row.id);
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    //删除接口
    async deleteMethod(id) {
      let parames = {
        handshake: "",
        data: {
          id: id
        }
      };
      let res = await this.$axios.post(
        this.$api.shareDelete,
        JSON.stringify(parames)
      );
      if (res.code == 200) {
        this.$message({
          message: res.message,
          type: "success"
        });
        this.getDataList();
      } else {
        this.$message({
          message: res.message,
          type: "warning"
        });
      }
    }
  }
};
</script>
<style scoped>
h3 {
  padding: 0.2rem;
  color: #3c763d;
}
.search {
  display: flex;
  margin-top: 0.2rem;
  padding-left: 0.2rem;
  width: 100%;
  flex-wrap: wrap;
}
.el-input {
  width: 2.5rem;
}
label {
  width: 0.83rem;
  height: 0.4rem;
  display: flex;
  justify-content: center;
  align-items: center;
  background: #f5f7fa;
  color: #909399;
  border: 1px solid #dcdfe6;
  border-top-left-radius: 4px;
  border-bottom-left-radius: 4px;
  font-size: 14px;
}
.cond {
  display: flex;
  margin-right: 0.4rem;
  margin-bottom: 0.1rem;
}
.search .el-select >>> .el-input__inner {
  border-left: none;
  border-top-left-radius: 0px;
  border-bottom-left-radius: 0px;
}
.search .el-select {
  width: 1.67rem;
}
.opration {
  margin-top: 0.2rem;
  padding-left: 0.2rem;
}
.page {
  margin-top: 0.2rem;
  display: flex;
  justify-content: center;
}
.list {
  margin-top: 0.2rem;
  padding: 0 0.1rem;
  font-size: 0.12rem;
}
.dialogBtn {
  display: flex;
  justify-content: center;
  margin-top: 0.3rem;
}
.row {
  margin-bottom: 0.1rem;
  display: flex;
  align-items: center;
}
.row span {
  width: 1rem;
  display: block;
}
</style>